package com.moon.cloud.common.response;

import org.springframework.http.HttpStatus;

/**
 * 统一返回格式
 */
public class Response {

    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";
    private static final String DEFAULT_FAIL_MESSAGE = "操作失败";

    // 成功响应
    public static <T> ResponseEntity<T> success() {
        return success(DEFAULT_SUCCESS_MESSAGE);
    }

    public static <T> ResponseEntity<T> success(String message) {
        return buildResponse(HttpStatus.OK.value(), true, message, null);
    }

    public static <T> ResponseEntity<T> success(T data) {
        return success(DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static <T> ResponseEntity<T> success(String message, T data) {
        return buildResponse(HttpStatus.OK.value(), true, message, data);
    }

    public static <T> ResponseEntity<T> success(Integer code, String message, T data) {
        return buildResponse(code, true, message, data);
    }

    // 失败响应
    public static <T> ResponseEntity<T> fail() {
        return fail(DEFAULT_FAIL_MESSAGE);
    }

    public static <T> ResponseEntity<T> fail(String message) {
        return fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
    }

    public static <T> ResponseEntity<T> fail(Integer code, String message) {
        return buildResponse(code, false, message, null);
    }

    public static <T> ResponseEntity<T> fail(Integer code, String message, T data) {
        return buildResponse(code, false, message, data);
    }

    // 通用构建方法
    private static <T> ResponseEntity<T> buildResponse(Integer status, Boolean isSuccess, String message, T data) {
        ResponseEntity<T> response = new ResponseEntity<>();
        response.setStatus(status);
        response.setIsSuccess(isSuccess);
        response.setMessage(message);
        if (data != null) {
            response.setData(data);
        }
        return response;
    }
}
